library(stargazer)
library(effects)
library(ggplot2)
library(scales)
library("grid")
library(xtable)

load("final/study2.RData")



data <- study2
data$pid <- droplevels(data$pid)
sink("final/s2_race.tex")
latex(  tabular(  (Race=race) ~  (Percent("col")+ 1)    ,data=data        ))
sink()

sink("final/s2_gender.tex")
latex(  tabular(  (Gender=gender) ~  (Percent("col")+ 1)    ,data=data        ))
sink()

sink("final/s2_edu.tex")
latex(  tabular(  (Education=education) ~  (Percent("col")+ 1)    ,data=data        ))
sink()

sink("final/s2_pid.tex")
latex(  tabular(  (PartyID=pid) ~  (Percent("col")+ 1)    ,data=data        ))
sink()

#\subsection{Model Used to Generate Figure 2}
model<-glm(op~affectivepolarization+gender+race+age+income+education+married+christian+pid,family=binomial(logit),data)
eff<-effect(model,term="affectivepolarization",as.table=T,default.levels=100,typical = "median",xlevels=list(affectivepolarization=seq(0, 1, by=.01)))
dataeff<-as.data.frame(eff)

d<-ggplot(data=dataeff, aes(x=affectivepolarization, y=fit)) + geom_line() + geom_ribbon(aes(ymin=lower, ymax=upper),alpha=0.2,linetype=0)+ scale_y_continuous(limits=c(0, 1))+ scale_x_continuous(limits=c(0, 1)) + xlab("Affective Polarization of the Participant")+ylab("Predicted Probability of Selecting an\n Opposing Partisan Team Member") + theme_bw() + scale_colour_manual(values=c("#4d4d4d","#4d4d4d")) 
d<-d  +theme(panel.margin = unit(1, "lines"))+ theme(panel.grid.major = element_line(colour = "white"),panel.grid.minor = element_line(colour = "white"),axis.title.x = element_text(vjust=-0.5)) +theme(legend.title=element_blank())  
ggsave(filename="final/opTeam.pdf", plot=d,width=4.5,height=4)

model<-glm(co~affectivepolarization+gender+race+age+income+education+married+christian+pid,family=binomial(logit),data)
eff<-effect(model,term="affectivepolarization",as.table=T,default.levels=100,typical = "median",xlevels=list(affectivepolarization=seq(0, 1, by=.01)))
dataeff<-as.data.frame(eff)

d<-ggplot(data=dataeff, aes(x=affectivepolarization, y=fit)) + geom_line() + geom_ribbon(aes(ymin=lower, ymax=upper),alpha=0.2,linetype=0)+ scale_y_continuous(limits=c(0, 1))+ scale_x_continuous(limits=c(0, 1)) + xlab("Affective Polarization of the Participant")+ylab("Predicted Probability of Selecting a\n Co-Partisan Team Member") + theme_bw() + scale_colour_manual(values=c("#4d4d4d","#4d4d4d")) 
d<-d  +theme(panel.margin = unit(1, "lines")) + theme(panel.grid.major = element_line(colour = "white"),panel.grid.minor = element_line(colour = "white"),axis.title.x = element_text(vjust=-0.5)) +theme(legend.title=element_blank())                       
d 

ggsave(filename="final/coTeam.pdf", plot=d,width=4.5,height=4)

#\input{tables/avoid_for_appendix.tex}

m1 <-glm(co~affectivepolarization,family=binomial(logit),data)
m2 <-glm(co~affectivepolarization+gender+race+age+income+education+married+christian+pid,family=binomial(logit),data)
m3 <-glm(op~affectivepolarization,family=binomial(logit),data)
m4 <-glm(op~affectivepolarization+gender+race+age+income+education+married+christian+pid,family=binomial(logit),data)
stargazer(m2,m4,ci = T)
library(stargazer)
stargazer(m1,m2,m3,m4,covariate.labels = c("Affective Polarization","Female","White","Age","Income: 30-59k","Income: 60-79k","Income: 80k+","Education: College+","Education: Some College","Not Married","Not Christian","Republican","Intercept"),out = "final/avoid_for_appendix.tex",no.space = T,column.labels = c("In-Party","In-Party","Out-Party","Out-Party"),dep.var.caption = "Condition",model.numbers = F,title = "Relationship between affective polarization and avoiding the in-party member/out-party member",dep.var.labels.include = F,star.cutoffs = c(.05,.01,.001),multicolumn = T)

#\subsection{Model separated by Party ID of respondent}
#\input{tables/avoid_for_appendix_byparty.tex}

m1 <-glm(co~affectivepolarization+gender+race+age+income+education+married+christian,family=binomial(logit),subset(data,pid=='Republican'))
m2 <-glm(op~affectivepolarization+gender+race+age+income+education+married+christian,family=binomial(logit),subset(data,pid=='Republican'))


m3 <-glm(co~affectivepolarization+gender+race+age+income+education+married+christian,family=binomial(logit),subset(data,pid=='Democrat'))
m4 <-glm(op~affectivepolarization+gender+race+age+income+education+married+christian,family=binomial(logit),subset(data,pid=='Democrat'))

stargazer(m1,m2,m3,m4,covariate.labels = c("Affective Polarization","Female","White","Age","Income: 30-59k","Income: 60-79k","Income: 80k+","Education: College+","Education: Some College","Not Married","Not Christian","Intercept"),out = "final/avoid_for_appendix_byparty.tex",no.space = T,column.labels = c("In-Party","Out-Party","In-Party","Out-Party"),dep.var.caption = "",dep.var.labels = c("Republicans","Republicans","Democrats","Democrats"),model.numbers = F,title = "Relationship between affective polarization and avoiding the in-party member/out-party member",star.cutoffs = c(.05,.01,.001),multicolumn = T)

